﻿using Devil;
using GameToolkit.ContentProvider;
using UnityEngine;

namespace GameToolkit
{
    [System.Serializable]
    public struct TableReference : System.IEquatable<TableReference>
    {
        // the asset's guid (only used in editor)
        [SerializeField, HideInInspector]
        string m_File;

        [SerializeField]
        int m_Id;

        public T GetData<T>(TableSet<T> set = null) where T : TableBase, new()
        {
            if (set == null)
                return TableSet<T>.Instance[m_Id];
            else
                return set[m_Id];
        }

        public bool Equals(TableReference other)
        {
            return this.m_Id == other.m_Id && this.m_File.EqualsIgnoreCase(other.m_File);
        }

        public static implicit operator int(TableReference refInst)
        {
            return refInst.m_Id;
        }

        public static implicit operator TableReference(int id)
        {
            TableReference tref;
            tref.m_Id = id;
            tref.m_File = null;
            return tref;
        }
    }
}
